﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Data.Dynamic;
using EPiServer.Licensing;

namespace Auros.EPiSiteMap.Data
{
    [EPiServerDataStore(
        StoreName = "SiteMapHostConfigDataStore",
        AutomaticallyCreateStore = true,
        AutomaticallyRemapStore = true)]
    public class SiteMapHostConfig
    {
        public bool Enabled { get; set; }
        public string SiteID { get; set; }
        public string Host { get; set; }

        public string FileName { get; set; }
        public string[] Langauges { get; set; }

        public DateTime LastCreated { get; set; }
        public int NumberOfEntries { get; set; }

        private static DynamicDataStore GetStore()
        {
            return DynamicDataStoreFactory.Instance.GetStore(typeof(SiteMapHostConfig));
        }
        
        /// <summary>
        ///   Returns the sites and hosts from the configuration.
        /// </summary>
        public static IEnumerable<SiteMapHostConfig> GetSitesAndHosts()
        {
            var sites = SiteInformationHandler.GetSitesInformation(true);

            var storeItems = GetStore().LoadAll<SiteMapHostConfig>();

            foreach (var site in sites)
            {
                foreach (var host in site.SiteHosts)
                {
                    if (host.SiteHost != "*")
                    {
                        var storeItem = storeItems.FirstOrDefault(item => item.SiteID == site.SiteId && item.Host == host.SiteHost && item.Enabled);

                        if (storeItem != null)
                            yield return storeItem;
                    }
                }
            }
        }

        /// <summary>
        ///   Returns the selected languages for the sitemap matched with the enabled languages on the site.
        /// </summary>
        public string[] GetEnabledLanguageSelections()
        {
            // Could be that language selected for a sitemap are no longer available on the site.
            return
                EPiServer.DataAbstraction.LanguageBranch.ListEnabled()
                    .Where(lang => this.Langauges.Contains(lang.LanguageID))
                        .Select(lang => lang.LanguageID)
                            .ToArray();
        }
    
        public static void Save(SiteMapHostConfig data)
        {
            var store = GetStore();

            SiteMapHostConfig record = store.LoadAll<SiteMapHostConfig>().FirstOrDefault(item => item.SiteID == data.SiteID && item.Host == data.Host);

            if(record != null)
            {
                record.Langauges = data.Langauges;
                record.LastCreated = data.LastCreated;
                record.NumberOfEntries = data.NumberOfEntries;
            }
            else
            {
                record = data;
            }

            store.Save(record);
        }
    }
}